package com.university.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.university.infrastructure.Result;
import com.university.pojo.cqe.command.CmdSaveFood;
import com.university.pojo.cqe.command.CmdUpdateFood;
import com.university.pojo.cqe.query.FoodQuery;
import com.university.pojo.dto.FoodAdminDTO;
import com.university.pojo.dto.FoodUserDTO;
import com.university.pojo.entity.Food;
import com.university.service.FoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Date: 2021/5/11 16:58
 * @Description: 食品饮料
 */
@Api(tags = {"食品饮料接口"})
@RestController
public class FoodController extends BaseController {

    @Resource
    private FoodService foodService;

    @ApiOperation("管理员添加")
    @PostMapping("/admin/saveFood")
    public Result saveFood(@RequestBody CmdSaveFood cmdSaveFood) {
        foodService.saveFood(cmdSaveFood);
        return ok();
    }

    @ApiOperation("管理员根据id修改")
    @PutMapping("/admin/updateFood")
    public Result updateFood(@RequestBody CmdUpdateFood cmdUpdateFood) {
        foodService.updateFood(cmdUpdateFood);
        return ok();
    }

    @ApiOperation("管理员删除")
    @DeleteMapping("/admin/deleteFood")
    public Result deleteFood(String id) {
        foodService.deleteFood(id);
        return ok();
    }

    @ApiOperation("管理员查询")
    @GetMapping("/admin/getFood")
    public Result adminGetFood(String type, String title,
                               @RequestParam(defaultValue = "1") Long page,
                               @RequestParam(defaultValue = "10") Long pageSize) {
        FoodQuery query = new FoodQuery();
        query.setTitle(title);
        query.setType(type);
        query.setPage(page);
        query.setPageSize(pageSize);
        IPage<Food> foodPage = foodService.adminGetFoodPage(query);
        return ok(foodPage, FoodAdminDTO.class);
    }

    @ApiImplicitParam(name = "type", value = "类型", required = true, dataTypeClass = String.class)
    @ApiOperation("用户根据类型查询食品饮料")
    @GetMapping("/user/getFood")
    public Result userGetFood(String type) {
        FoodQuery query = new FoodQuery();
        query.setType(type);
        List<FoodUserDTO> foodList = foodService.userGetFood(query);
        return ok(foodList);
    }

}
